package ltd.smallwang.infrastructure.user.db.model;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import ltd.smallwang.domain.user.User;
import ltd.smallwang.infrastructure.util.CryptoUtil;

import java.time.LocalDateTime;

@Data
@TableName("sw_user")
public class UserPO {

  @TableId(type = IdType.AUTO)
  private Long id;

  private String username;

  private String password;

  private String nickName;

  private String avatar;

  private String email;

  private String emailHash;

  private String platform;

  private Integer sex;

  private LocalDateTime registerTime;

  private LocalDateTime modifyTime;

  public static UserPO convertToUserPO(User user) {
    UserPO userPO = new UserPO();
    userPO.setId(user.getId());
    userPO.setUsername(user.getUsername());
    userPO.setPassword(user.getPassword());
    userPO.setNickName(user.getNickName());
    userPO.setAvatar(user.getAvatar());
    userPO.setEmail(CryptoUtil.encrypt(user.getEmail()));
    userPO.setEmailHash(DigestUtil.sha256Hex(user.getEmail()));
    userPO.setPlatform(user.getPlatform().getName());
    userPO.setSex(user.getSexEnum().getSex());
    userPO.setRegisterTime(user.getRegisterTime());
    userPO.setModifyTime(user.getModifyTime());
    return userPO;
  }
}
